package testssss;

import com.wsj.demo.entity.Evection;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * yao
 * 测试流程变量
 */
public class TestVariables1 {
    /**
     * 测试流程部署
     */
    @Test
    public void testDeployment() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("diagram/ceshi.bpmn20.xml")
                .name("出差申请")
                .deploy();
        System.out.println(deployment.getName());
        System.out.println(deployment.getId());
    }
    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String, Object> map = new HashMap<>();
        Evection evention = new Evection();
        evention.setNum(1);
        map.put("evention", evention);
        map.put("assignee0", "张三1");
        map.put("assignee1", "部门经理1");
        map.put("assignee2", "总经理1");
        map.put("assignee3", "财务员1");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("chuchai",map);
        System.out.println("流程实例ID:" + processInstance.getId());
        System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());
        System.out.println("流程定义名称:" + processInstance.getProcessDefinitionName());
    }
    /**
     * 查询当前个人待执行的任务
     */
    @Test
    public void testFindPersonalTaskList() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //processDefinitionKey 流程key  taskAssignee 任务负责人
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("chuchai").taskAssignee("CAIWU2").list();
        for (Task task : list) {
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务负责人:" + task.getAssignee());
            System.out.println("任务名称:" + task.getName());
        }

    }
    /**
     * 完成我的任务
     */
    @Test
    public void testCompleteMyPersonalTask() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //只返回一个值
        Task task = taskService.createTaskQuery().processDefinitionKey("chuchai").taskAssignee("张三1").singleResult();
        Map<String, Object> map = new HashMap<>();
        map.put("assignee1", "CAIWU2");
        //任务ID
        taskService.complete( task.getId(),map);
        System.out.println( "完成任务");
    }
}
